Skip to content

Fix relative format showing now for any past time under a minute - #390

Merged
liuliu-dev merged 2 commits into
github:mainfrom
kwy404:fix-relative-seconds-ago
Nov 4, 2026
Merged

liuliu-dev merged 2 commits into
github:mainfrom
kwy404:fix-relative-seconds-ago

Conversation

@kwy404

@kwy404 kwy404 commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Fixes #269

#getRelativeFormat in src/relative-time-element.ts turns anything under 10 seconds into "now":

if (unit === 'second' && int < 10) {

For past dates int is negative, so this is always true and every past time under a minute renders as "now". Future dates are fine, which is why the table tests expect "in 50 seconds" for 50 seconds ahead but "now" for 50 seconds ago.

<!-- 30 seconds ago -->
<relative-time datetime="..." format="relative"></relative-time>
<!-- before: now, after: 30 seconds ago -->

Before #getRelativeFormat switched to durations, timeAgo returned "now" under 10 seconds and "N seconds ago" from 10 to 44 seconds, so this looks like a regression from that change. The fix compares Math.abs(int).

I updated the two "50 seconds in the past" table tests that encoded the old output. They fail before the change and pass after. I ran the full test suite (web-test-runner, Chromium), tsc --noEmit and eslint.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the fix restoring second-level relative output for past timestamps.

No open issues 🎉

What changed in this PR

Fixes #269 by restoring second-level relative output for past timestamps instead of incorrectly displaying “now” throughout the preceding minute.

Changes:

  • Uses the absolute seconds value for the “now” threshold, treating past and future timestamps consistently.
  • Updates the 50-seconds-ago test expectations for standard and narrow formatting.
File Description
test/​relative-time.js Expects “50 seconds ago” and “50s ago” in the existing regression cases.
src/​relative-time-element.ts Applies Math.abs(int) to the under-10-second threshold.

Review effort: Max

@liuliu-dev
liuliu-dev merged commit 79b42ad into github:main Nov 4, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot get format=relative to display seconds

3 participants